﻿'
' Version:  26.05.2007 /BJ  Button von ...Löschen auf ...Delete umbenannt
'
Partial Class benutzer
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Roles.IsUserInRole("admin") Then
            Response.Redirect("~/login.aspx")
        End If
    End Sub
    Public Sub cboUserBind()
        cboUser.DataSource = Membership.GetAllUsers
        cboUser.DataBind()
    End Sub

    Public Sub ddlUserPasswordBind()
        ddlUserPassword.DataSource = Membership.GetAllUsers
        ddlUserPassword.DataBind()
    End Sub
    Public Sub cboUserBind1()
        cboUserRollenAuswahl.DataSource = Membership.GetAllUsers
        cboUserRollenAuswahl.DataBind()
        cboUserRollenAuswahl1.DataSource = Membership.GetAllUsers
        cboUserRollenAuswahl1.DataBind()
    End Sub
    Public Sub cboRollenBind()
        cboRollen.DataSource = Roles.GetAllRoles
        cboRollen.DataBind()
    End Sub
    Public Sub AlleRollenBind()
        ddlRollen.DataSource = Roles.GetAllRoles
        ddlRollen.DataBind()
    End Sub
    Public Sub UserInRollen()
        ddlUserInRollen.DataSource = Roles.GetRolesForUser(cboUserRollenAuswahl.Text)
        ddlUserInRollen.DataBind()
    End Sub
    Protected Sub butUserNeu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butUserNeu.Click
        Dim result As MembershipCreateStatus
        If UserPass.Text <> UserPassW.Text Then
            Msg.Text = "Passwort nicht gleich"
        Else
            Membership.CreateUser(UserName.Text, UserPass.Text)
            Select Case result
                Case MembershipCreateStatus.Success
                    UserName.Text = Nothing
                    UserPass.Text = Nothing
                    UserPassW.Text = Nothing
                    Msg.Text = ""
                    cboUserBind()
                    cboUserBind1()
                Case Else
                    Msg.Text = "Benutzer konnte nicht angelegt werden"
            End Select
        End If
    End Sub

    Protected Sub butRolleNeu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butRolleNeu.Click
        Roles.CreateRole(texRolleNeu.Text)
        cboRollenBind()
        AlleRollenBind()
        texRolleNeu.Text = ""
    End Sub

    Protected Sub Rollen_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboRollen.Init
        cboRollenBind()
    End Sub

    Protected Sub User_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboUser.Init
        cboUserBind()
    End Sub

    Protected Sub delRolle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles delRolle.Click
        If Roles.GetUsersInRole(cboRollen.Text).Length > 0 Then
            Msg.Text = "Rolle nicht leer!"
        Else
            Roles.DeleteRole(cboRollen.Text)
            cboRollenBind()
            AlleRollenBind()
        End If
    End Sub

    Protected Sub butDelUser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butDelUser.Click
        Membership.DeleteUser(cboUser.Text)
        cboUserBind()
        cboUserBind1()
    End Sub
    Protected Sub butUserZuRollenDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butUserZuRollenDelete.Click
        Roles.RemoveUserFromRole(cboUserRollenAuswahl.Text, ddlUserInRollen.SelectedValue)
        UserInRollen()
    End Sub

    'Protected Sub butUserRolleAuswahl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butUserRolleAuswahl.Click
    '    UserInRollen()
    'End Sub

    Protected Sub AlleRollen_INIT(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRollen.Init
        AlleRollenBind()
    End Sub

    Protected Sub butAddUserToRole_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butAddUserToRole.Click
        Roles.AddUserToRole(cboUserRollenAuswahl.SelectedValue, ddlRollen.SelectedValue)
        UserInRollen()
    End Sub

    Protected Sub cboUserRollenAuswahl_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboUserRollenAuswahl.Init
        cboUserBind1()
    End Sub

    Protected Sub ddlUserInRollen_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUserInRollen.Init
        UserInRollen()
    End Sub


    Protected Sub ddUserPassword_Init(sender As Object, e As EventArgs) Handles ddlUserPassword.Init
        ddlUserPasswordBind()
    End Sub

    Protected Sub butChangePassword_Click(sender As Object, e As EventArgs) Handles butChangePassword.Click

        Dim u As MembershipUser = Membership.GetUser(Me.ddlUserPassword.SelectedValue)
        Try

            If u.ChangePassword(txtOldPassword.Text, txtNewPassword.Text) Then
                Msg.Text = "Password changed."
            Else
                Msg.Text = "Password change failed. Please re-enter your values and try again."
            End If
        Catch ex As Exception
            Msg.Text = "An exception occurred: " & Server.HtmlEncode(ex.Message) & ". Please re-enter your values and try again."
        End Try
       
    End Sub

    Private Sub Bind()
        ddlUserPasswordBind()
        cboUserBind1()
        AlleRollenBind()
    End Sub
End Class
